import Foundation

/// 福利活动模型
struct Activity: Identifiable, Codable {
    let id: UUID
    let name: String
    let website: String
    let imageUrl: String
    let startDate: Date
    let endDate: Date
    var isActive: Bool {
        let now = Date()
        return now >= startDate && now <= endDate
    }
    
    init(id: UUID = UUID(), 
         name: String, 
         website: String, 
         imageUrl: String, 
         startDate: Date, 
         endDate: Date) {
        self.id = id
        self.name = name
        self.website = website
        self.imageUrl = imageUrl
        self.startDate = startDate
        self.endDate = endDate
    }
}

// MARK: - 日期格式化

extension Activity {
    var dateRangeString: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return "\(formatter.string(from: startDate)) 至 \(formatter.string(from: endDate))"
    }
    
    var daysRemaining: Int {
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day], from: Date(), to: endDate)
        return max(components.day ?? 0, 0)
    }
}

// MARK: - 预设活动

extension Activity {
    /// 黑色序章宿命回响活动
    static let blackPrelude: Activity = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        return Activity(
            name: "黑色序章宿命回响",
            website: "https://dnf.qq.com/cp/a20250807index/indexm.html?ADTAG=media.buy.baidukeyword.PC_dnf_DNF_u34669862.k1126102684040.a124111044398&e_code=539051",
            imageUrl: "https://game.gtimg.cn/images/dnf/cp/a20250807index/header.jpg",
            startDate: formatter.date(from: "2025-08-28") ?? Date(),
            endDate: formatter.date(from: "2025-09-25") ?? Date()
        )
    }()
    
    /// 2025DNF井盖杯活动
    static let manholeCoverCup2025: Activity = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        return Activity(
            name: "2025DNF井盖杯",
            website: "https://dnf.qq.com/cp/a20250911cover/",
            imageUrl: "https://imagetest.hdnf.qq.com/a20250822manholecover/images/header_bg-DArlyEN2.png",
            startDate: formatter.date(from: "2025-08-11") ?? Date(),
            endDate: formatter.date(from: "2025-09-12") ?? Date()
        )
    }()
    
    /// 所有预设活动
    static let allActivities: [Activity] = [
        blackPrelude,
        manholeCoverCup2025
    ]
}